package com.plwf.lake.controller;
import com.plwf.lake.entity.DriverSchool;
import com.plwf.lake.service.DriverSchoolService;
import com.plwf.lake.util.Uploader;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 驾校基本信息接口
 * @author:plwf
 * @time:2019-5-12
 */
@RestController
@RequestMapping("/school")
public class DriverSchoolCtrl {

    @Resource
    private DriverSchoolService service;

    //图片存储路径
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**/**").addResourceLocations("file:F:/gradimages/lake/");

    }

    //保存驾校信息
    @RequestMapping("/save")
    public DriverSchool save(DriverSchool bean){
        if(service.save(bean)){
            return bean;
        }else{
            return null;
        }

    }

    //通过id删除驾校信息
    @RequestMapping("/remove/{id}")
    public boolean remove(@PathVariable("id") Integer id){
        return service.remove(id);
    }

    //通过id查找驾校信息
    @RequestMapping("/find/{id}")
    public DriverSchool find(@PathVariable("id") Integer id ){
        return service.find(id);
    }

    //查找所有驾校信息
    @RequestMapping("/findAll")
    public List<DriverSchool> findAll(){
        return service.queryAll();
    }


    //修改驾校状态码
    @RequestMapping("enable/{id}/{status}")
    public boolean enable(@PathVariable("id") Integer id,@PathVariable("status") Integer status){
        return service.enable(id,status);

    }

    //驾校图片上传
    @RequestMapping("/uploads")
    public boolean uploads(MultipartFile picture,MultipartFile pictureb,MultipartFile picturec,MultipartFile pictured, HttpServletRequest request){
        //获取图片存储的绝对系统路径aa
        String path = "F:/gradimages/lake";
        String id = request.getParameter("id");
        //
        String ctrl = "upload-";
        Integer sid = Integer.parseInt(id);
        DriverSchool driverSchool =  new DriverSchool();

        if(picture!=null && picture.getSize()>0){
            String pa = Uploader.upload(picture,path,ctrl,sid);
            if(pa!=null){
                driverSchool.setSpice(pa);
                driverSchool.setId(Integer.parseInt(id));
            }
        }
        if(pictureb!=null && pictureb.getSize()>0){
            String pb = Uploader.upload(pictureb,path,ctrl,sid);
            if(pb!=null){
                driverSchool.setSpiceb(pb);
                driverSchool.setId(Integer.parseInt(id));
            }
        }
        if(picturec!=null && picturec.getSize()>0){
            String pc = Uploader.upload(picturec,path,ctrl,sid);
            if(pc!=null){
                driverSchool.setSpicec(pc);
                driverSchool.setId(Integer.parseInt(id));
            }
        }
        if(pictured!=null && pictured.getSize()>0){
            String pd = Uploader.upload(pictured,path,ctrl,sid);
            if(pd!=null){
                driverSchool.setSpiced(pd);
                driverSchool.setId(Integer.parseInt(id));
            }
        }
        return service.updateImage(driverSchool);

    }
}
